package com.situ.mall.controller;

import com.situ.mall.pojo.Category;
import com.situ.mall.service.ICategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class IndexController {
    @Autowired
    private ICategoryService categoryService;

    private Map<String, List<Category>> map = new HashMap<>();

    @RequestMapping("/")
    public String index(Model model) {
        List<Category> topCategoryList = map.get("topCategoryList");
        if (CollectionUtils.isEmpty(topCategoryList)) {//缓存里面没有
            topCategoryList = categoryService.selectTopCategoryList();
            //把数据更新到缓存里面
            map.put("topCategoryList", topCategoryList);
        }
        List<Category> secondCategoryList = categoryService.selectSecondCategoryList();
        model.addAttribute("topCategoryList", topCategoryList);
        model.addAttribute("secondCategoryList", secondCategoryList);
        return "index";
    }

    @RequestMapping("/1")
    public String index1(Model model) {
        List<Category> topCategoryList = categoryService.selectTopCategoryList();
        List<Category> secondCategoryList = categoryService.selectSecondCategoryList();
        model.addAttribute("topCategoryList", topCategoryList);
        model.addAttribute("secondCategoryList", secondCategoryList);
        return "index";
    }
}
